package DemoTest.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import DemoTest.entity.ResultVO;
import DemoTest.entity.Student;

@RestController
@RequestMapping("/student")
public class StudentController {
	/**
	 * condition的条件满足时，进行缓存，如果没有满足，则不进行缓存
	 * condition ="#name.length() > 3"
	 * public ResultVO list(@RequestParam(value = "name",required = false) String name)
	 * ********************
	 * 经过实测以及回顾知识得出结论（回看视频，哈哈，啊耶，好困）#result 表示resultVO的对象
	 * unless：当改属性为false时触发redis缓存，当为true时不进行缓存
	 * 具体有什么问题，记得问我噢，我很抱歉昨天没有及时帮你处理，而我也得到相应的教训，现在才弄好，呜呜呜，嘤嘤嘤~
	 * 还有主要是那个病毒又出来了，导致弄得很晚，所以连接的也是我的服务器
	 * @param name
	 * @return
	 */
	@GetMapping("/list")
	@Cacheable(cacheNames = "product",key = "123",unless="#result.getCode() == 0")
	public ResultVO list(@RequestParam(value = "name",required = false) String name){
		System.out.println("如果不是第一次进入,那么就是没有进行缓存....");
		ResultVO resultVO = new ResultVO();
		resultVO.setCode(0);
		resultVO.setMsg("成功");
		List<Student> students = new ArrayList<Student>();
		students.add(new Student(1,"ej1"));
		students.add(new Student(2,"ej2"));
		students.add(new Student(3,"ej3"));
		students.add(new Student(4,"ej4"));
		resultVO.setData(students);
		return resultVO;
	}
}
